package com.lkk.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.lkk.data.ExcelDataEntity;

/**
 * excel实体对象解析类
 * @author admin
 *
 */
public class EntityListUtil {
	/**
	 * 解析excel实体对象的头部信息
	 * @param exDataEntity
	 * @return
	 */
	public Map<String, String> getHeader(ExcelDataEntity exDataEntity) {
		//获取实体类的头部信息
		String headerString = exDataEntity.getHeader();
		//存储头部信息的map
		Map<String,String> headerMap = new HashMap<String, String>();
		//切割用的String
		String[] result;
		if(headerString.indexOf("\n")!=-1) {
			//根据换行符切割成数组
			String[] headerArrays = headerString.split("\n|\r");
			for(int i = 0;i<headerArrays.length;i++) {
				result = headerArrays[i].split(":");
				headerMap.put(result[0], result[1]);
			}	
		}else {
			result = headerString.split(":");
			headerMap.put(result[0], result[1]);
		}
		return headerMap;
	}
	
	/**
	 * 从excel实体对象中取出JSONObject格式的body对象
	 * @param exDataEntity excel实体对象
	 * @return
	 */
	public JSONObject parserBody(ExcelDataEntity exDataEntity) {
		//获取String格式的body
		String bodyString = exDataEntity.getBody();
		//转为JSONObject格式
		JSONObject jo = JSON.parseObject(bodyString);
		return jo;
	}
	
	/**
	 * 从excel实体对象中取出JSONObject格式的body对象,并在body中添加token
	 * @param exDataEntity
	 * @param token
	 * @return
	 */
	public JSONObject parserBody(ExcelDataEntity exDataEntity,String token) {
		String bodyString = exDataEntity.getBody();
		JSONObject jo = JSON.parseObject(bodyString);
		//设置token参数
		jo.put("token", token);
		return jo;
	}
	
	/**
	 * 解析excel实体对象的断言集合
	 * @param exDataEntity
	 * @return
	 */
	public Map<String, String> getAssertions(ExcelDataEntity exDataEntity) {
		//获取实体类的断言集合信息
		String assertionsString = exDataEntity.getAssertions();
		//存储断言集合的map
		Map<String,String> assertionsMap = new HashMap<String, String>();
		//切割用的String
		String[] result;
		if(assertionsString.indexOf("\n")!=-1) {
			//根据换行符切割成数组
			String[] assertionsArrays = assertionsString.split("\n|\r");
			for(int i = 0;i<assertionsArrays.length;i++) {
				result = assertionsArrays[i].split(":");
				assertionsMap.put(result[0], result[1]);
			}	
		}else {
			result = assertionsString.split(":");
			assertionsMap.put(result[0], result[1]);
		}
		return assertionsMap;
	}
	

}
